"""
File: example_1_1_1_3.py
Project: 活动3_智能统计关键词
File Created: 2020/02/10 14:13:53
Author: Dunmore (fovd2020@outlook.com)
--------------------------------------------
Description:统计三国演义人物出现的频率
exclude_words.txt中包含了部分出现频率比较高的将被排除的词，每行一个
有些词如丞相，既有可能指曹操也有可能值诸葛亮，所以也被排除，同样的还有将军、都督等
--------------------------------------------
"""
import os
import jieba

currrent_dir = (
    os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()
)

# 获取exclude_words.txt中的排除的词语
exclude_list = (
    open(os.path.join(currrent_dir, "exclude_words.txt"), encoding="utf-8")
    .read()
    .splitlines()
)

# 读取三国演义的内容并用jieba分词
content = open(
    os.path.join(currrent_dir, "三国演义.txt"), "r", encoding="utf-8"
).read()
words = jieba.lcut(content)

# 记录词频
counts = {}

for word in words:
    r_word = word
    if len(word) == 1 or word in exclude_list:
        continue
    # 将同一个人的不同称呼转化为实际指向的人
    elif word in ["孟德", "阿瞒"]:
        r_word = "曹操"
    elif word in ["孔明曰", "诸葛亮"]:
        r_word = "孔明"
    elif word in ["玄德曰", "玄德"]:
        r_word = "刘备"
    elif word in ["关公", "云长", "关云长"]:
        r_word = "关羽"
    elif word in ["后主"]:
        r_word = "刘禅"

    # 计数+1
    counts[r_word] = counts.get(r_word, 0) + 1

# 转换成list并按照词频从高到低排序
result_list = list(counts.items())
result_list.sort(key=lambda x: x[1], reverse=True)

# 输出前20名人物
for index, r in enumerate(result_list[:20], start=1):
    print(f"第{index}名:{r[0]}[出现了{r[1]}次]")
